<template>
  <p-card :bordered="false">
    <!-- 查询区域 -->
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <p-form-item label="产品编码">
              <p-input placeholder="请输入产品编码" v-model="queryParam.mcode"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <p-form-item label="产品名称">
              <p-input placeholder="请输入产品名称" v-model="queryParam.mname"></p-input>
            </p-form-item>
          </p-col>

          <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <p-button type="primary" @click="searchQuery" icon="search">查询</p-button>
              <p-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</p-button>
            </span>
          </p-col>
        </p-row>
      </p-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <template v-if="cgButtonList && cgButtonList.length>0" v-for="(item,index) in cgButtonList">
        <p-button
          v-if=" item.optType=='js' && (item.groupTag == null || item.groupTag == '' )"
          :key=" 'cgbtn'+index "
          @click="cgButtonJsHandler(item.buttonCode)"
          type="primary"
          :icon="item.buttonIcon"
        >{{ item.buttonName }}</p-button>
        <p-button
          v-else-if=" item.optType=='action' && (item.groupTag == null || item.groupTag == '' )"
          :key=" 'cgbtn'+index "
          @click="cgButtonActionHandler(item.buttonCode)"
          type="primary"
          :icon="item.buttonIcon"
        >{{ item.buttonName }}</p-button>
      </template>

      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="fieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>

      <p-dropdown v-for="(item,index) in groupTag" :key="index">
        <p-button>
          {{item.name}}
          <p-icon type="down" />
        </p-button>
        <p-menu slot="overlay">
          <template v-for="(items,indexs) in item.data">
            <p-menu-item
              v-if=" items.optType=='js' "
              :key="indexs"
              @click="cgButtonJsHandler(items.buttonCode)"
            >
              <p-icon :type="items.buttonIcon" />
              {{items.buttonName}}
            </p-menu-item>
            <p-menu-item
              v-else-if=" items.optType=='action' "
              :key="indexs"
              @click="cgButtonActionHandler(items.buttonCode)"
            >
              <p-icon :type="items.buttonIcon" />
              {{items.buttonName}}
            </p-menu-item>
          </template>
        </p-menu>
      </p-dropdown>
    </div>
    <!-- table区域-begin -->
    <div>

      <p-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="defColumns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'radio'}"
        @change="handleTableChange"        
      >
        <span slot="action" slot-scope="text, record">
          <span v-if="record.hasSpecification" style="color: #ddd;">新增规格</span>
          <a v-else @click="showAddSprcModal(record)">新增规格</a>          
        </span>
      </p-table>
    </div>
    <!-- table区域-end -->

    <!-- 新增规格弹窗  -->
    <p-modal width='600px' v-model="isShowAddSprcModal" title="新增规格" @ok="okAddSprc">
      <p-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="{
          span: 8
        }"
        :wrapper-col="wrapperCol"
      >

        <p-form-model-item ref="boxLength"         
          label="纸箱长:" prop="boxLength">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.boxLength"
            min="1"
            @blur="
              () => {
                $refs.boxLength.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item>

        <p-form-model-item ref="boxWidth" label="纸箱宽" prop="boxWidth">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.boxWidth"
            min="1"
            @blur="
              () => {
                $refs.boxWidth.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item>



        <p-form-model-item ref="boxHigh" label="纸箱高" prop="boxHigh">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.boxHigh"
            min="1"
            @blur="
              () => {
                $refs.boxHigh.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item> 


        <p-form-model-item ref="bindNumber" label="捆扎次数" prop="bindNumber">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.bindNumber"
            min="1"
            @blur="
              () => {
                $refs.bindNumber.onFieldBlur();
              }
            "
          />
        </p-form-model-item>


        <p-form-model-item ref="stackingLayers" label="码垛层数" prop="stackingLayers">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.stackingLayers"
            min="1"
            @blur="
              () => {
                $refs.stackingLayers.onFieldBlur();
              }
            "
          />
        </p-form-model-item>

        <p-form-model-item ref="horizontalNumber" label="每层放置数" prop="horizontalNumber">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.horizontalNumber"
            min="1"
            @blur="
              () => {
                $refs.horizontalNumber.onFieldBlur();
              }
            "
          /> 
        </p-form-model-item>

        <p-form-model-item ref="horStrapNumber" label="水平捆包数" prop="horStrapNumber">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.horStrapNumber"
            min="1"
            @blur="
              () => {
                $refs.horStrapNumber.onFieldBlur();
              }
            "
          />
        </p-form-model-item>      
        <p-form-model-item ref="virStrapNumber" label="垂直捆包数" prop="virStrapNumber">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.virStrapNumber"
            min="1"
            @blur="
              () => {
                $refs.virStrapNumber.onFieldBlur();
              }
            "
          />
        </p-form-model-item> 
              
        <p-form-model-item ref="palletLength" label="托盘长度" prop="palletLength">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.palletLength"
            min="1"
            @blur="
              () => {
                $refs.palletLength.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item>             

        <p-form-model-item ref="palletWidth" label="托盘宽度" prop="palletWidth">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.palletWidth"
            min="1"
            @blur="
              () => {
                $refs.palletWidth.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item> 
        
        <p-form-model-item ref="angleHigh" label="护角高度" prop="angleHigh">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.angleHigh"
            min="1"
            @blur="
              () => {
                $refs.angleHigh.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item>        

        <p-form-model-item ref="coverPlateLength" label="盖板长度" prop="coverPlateLength">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.coverPlateLength"
            min="1"
            @blur="
              () => {
                $refs.coverPlateLength.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item> 

        <p-form-model-item ref="coverPlateWidth" label="盖板宽度" prop="coverPlateWidth">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.coverPlateWidth"
            min="1"
            @blur="
              () => {
                $refs.coverPlateWidth.onFieldBlur();
              }
            "
          /> mm
        </p-form-model-item> 

        <p-form-model-item ref="fullBoxSize" label="满盒数量" prop="fullBoxSize">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.fullBoxSize"
            min="1"
            @blur="
              () => {
                $refs.fullBoxSize.onFieldBlur();
              }
            "
          />
        </p-form-model-item>          

        <p-form-model-item ref="trunkFul" label="满箱数量" prop="trunkFul">
          <p-input-number
            :value="text"
            :precision="0"
            :formatter="limitNumber"
            :parser="limitNumber"
            v-model="form.trunkFul"
            min="1"
            @blur="
              () => {
                $refs.trunkFul.onFieldBlur();
              }
            "
          />
        </p-form-model-item>





      </p-form-model>
    </p-modal>
  </p-card>
</template>

<script type="text/javascript">
import { JeecgListMixin } from "@/mixins/JeecgListMixin";
import {
  deleteAction,
  getAction,
  downFile,
  postAction,
  httpAction,
} from "@/api/manage";
import JSuperQuery from "@/components/jeecg/JSuperQuery.vue";
import Vue from "vue";
import { api } from "@/api/manage";
import { filterObj } from "@/utils/util";
import {
  initDictOptions,
  filterMultiDictText,
} from "@/components/dict/JDictSelectUtil";

//高级查询modal需要参数

const superQueryFieldList = [
  {
    value: "mcode",
    text: "产品编码",
    type: "string",
  },
  {
    value: "mname",
    text: "产品名称",
    type: "string",
  },
  {
    value: "mexplain",
    text: "产品描述",
    type: "string",
  },
  {
    value: "mtypename",
    text: "产品系列",
    type: "string",
  },
  {
    value: "sprc",
    text: "产品规格",
    type: "string",
  },
];

export default {
  name: "PackingSpecificationMaintainTop",
  mixins: [JeecgListMixin],
  components: {
    JSuperQuery,
  },
  data() {
    return {
      fieldList: superQueryFieldList,
      superQueryFlag: false,
      superQueryParams: "",
      description: "规格",
      /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
      queryParam: {
        mclassid: "1",
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ["10", "20", "30"],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条";
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      defColumns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "产品编码",
          align: "center",
          dataIndex: "mcode",
        },
        {
          title: "产品名称",
          align: "center",
          dataIndex: "mname",
        },
        {
          title: "产品描述",
          align: "center",
          dataIndex: "mexplain",
        },
        {
          title: "产品系列",
          align: "center",
          dataIndex: "mtypename",
        },
        {
          title: "产品规格",
          align: "center",
          dataIndex: "sprc",
        },
        {
          title: "操作",
          dataIndex: "action",
          align: "center",
          scopedSlots: { customRender: "action" },
        },
      ],
      // 表头
      columns: [],
      labelCol: {
        span: 6,
      },
      wrapperCol: {
        span: 16,
      },
      labelCol2: {
        span: 3,
      },
      wrapperCol2: {
        span: 20,
      },

      //列设置
      settingColumns: [],
      addSprcrecord: {},
      isShowAddSprcModal: false,
      addSprcData: { fullBoxSize: "" },
      url: {
        list: "/basic-business/auto/baCpinfoMain/productList",
        delete: "/mes-craft/craft/mesPeDefinition/delete",
        deleteBatch: "/mes-craft/auto/mesPeProcessMaterialBasis/deleteBatch",
        exportXlsUrl: "/mes-craft/craft/mesPeDefinition/exportXls",
        importExcelUrl: "/mes-craft/auto/mesPeProcessMaterialBasis/importExcel",
        getColumns: "/mes-craft/online/cgform/api/getColumns/",
        buttonAction: "/mes-craft/online/cgform/api/doButton",
        addSprc: "/mes-craft/craft/mesPeSpecifications/add",
      },
      isShowPackingSpecificationMaintainModal: false,

      other: "",
      form: {
        boxLength: "",
        boxWidth: "",
        boxHigh: "",
        bindNumber: "",
        stackingLayers: "",
        horizontalNumber: "",
        horStrapNumber: "",
        virStrapNumber: "",
        palletLength: "",
        palletWidth: "",
        angleHigh: "",
        coverPlateLength: "",
        coverPlateWidth: "",
        trunkFul: "1",
        fullBoxSize: "1",
      },
      rules: {
        boxLength: [
          { required: true, message: "请输入纸箱长", trigger: "blur" },
        ],
        boxWidth: [
          { required: true, message: "请输入纸箱宽", trigger: "blur" },
        ],

        boxHigh: [{ required: true, message: "请输入纸箱高", trigger: "blur" }],

        bindNumber: [
          { required: true, message: "请输入捆扎次数", trigger: "blur" },
        ],
        stackingLayers: [
          { required: true, message: "码垛层数", trigger: "blur" },
        ],
        horizontalNumber: [
          { required: true, message: "每层放置数", trigger: "blur" },
        ],
        horStrapNumber: [
          { required: true, message: "水平捆包数", trigger: "blur" },
        ],
        virStrapNumber: [
          { required: true, message: "垂直捆包数", trigger: "blur" },
        ],

        palletLength: [
          { required: true, message: "托盘长度", trigger: "blur" },
        ],

        palletWidth: [{ required: true, message: "托盘宽度", trigger: "blur" }],
        angleHigh: [{ required: true, message: "护角高度", trigger: "blur" }],

        coverPlateLength: [
          { required: true, message: "盖板长度", trigger: "blur" },
        ],
        coverPlateWidth: [
          { required: true, message: "盖板宽度", trigger: "blur" },
        ],

        trunkFul: [{ required: true, message: "满箱数量", trigger: "blur" }],

        fullBoxSize: [{ required: true, message: "满盒数量", trigger: "blur" }],
      },
    };
  },
  mounted() {
    console.log(this.columns);
  },
  computed: {
    getWidth: function () {
      if (this.defColumns.length > 14) {
        return { width: "500px" };
      }
    },
    tableScroll: function () {
      if (this.columns.length > 13) {
        return { x: this.columns.length * 150 - 450 };
      }
    },
  },
  created() {
    // this.initColumns();
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      // 分页、排序、筛选变化时触发
      // TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field;
        this.isorter.order = sorter.order == "ascend" ? "asc" : "desc";
      }
      this.ipagination = pagination;

      this.loadData();
      this.selectedRowKeys = [];
      this.$parent.selectProductChange("");
    },

    onSelectChange(selectedRowKeys, selectionRows) {
      this.$parent.selectProductChange(selectionRows[0].mcode);
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    loadData(arg) {
      // debugger;
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!");
        return;
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams(); // 查询条件
      params.limit = params.pageSize;
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.data.records;
          this.ipagination.total = res.data.total;
        }
        if (res.code === 510) {
          this.$message.warning(res.msg);
        }
        this.$nextTick(() => {
          this.loading = false;
        });
      });
    },
    initColumns() {
      //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
      //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

      var key = this.$route.name + ":colsettings";
      let colSettings = Vue.ls.get(key);
      if (colSettings == null || colSettings == undefined) {
        let allSettingColumns = [];
        this.defColumns.forEach(function (item, i, array) {
          allSettingColumns.push(item.dataIndex);
        });
        this.settingColumns = allSettingColumns;
        this.columns = this.defColumns;
      } else {
        this.settingColumns = colSettings;
        const cols = this.defColumns.filter((item) => {
          if (item.key == "rowIndex" || item.dataIndex == "action") {
            return true;
          }
          if (colSettings.includes(item.dataIndex)) {
            return true;
          }
          return false;
        });
        this.columns = cols;
      }
    },
    //列设置更改事件
    onColSettingsChange(checkedValues) {
      var key = this.$route.name + ":colsettings";
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000);
      this.settingColumns = checkedValues;
      const cols = this.defColumns.filter((item) => {
        if (item.key == "rowIndex" || item.dataIndex == "action") {
          return true;
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true;
        }
        return false;
      });
      this.columns = cols;
    },
    //高级查询方法
    handleSuperQuery(arg) {
      if (!arg) {
        this.superQueryParams = "";
        this.superQueryFlag = false;
      } else {
        this.superQueryFlag = true;
        this.superQueryParams = JSON.stringify(arg);
      }
      this.loadData();
    },

    updateHasSpecification(mcode, isHasSpecification) {
      console.log("修改按钮状态");

      console.log(mcode);
      console.log(isHasSpecification);

      // 新增规则按钮置灰或可点击
      this.dataSource.forEach((ele) => {
        if (mcode == ele.mcode) {
          ele.hasSpecification = isHasSpecification;
        }
      });
    },

    okAddSprc() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          postAction(this.url.addSprc, this.form).then((res) => {
            if (res.success) {
              this.updateHasSpecification(this.form.productCode, true);

              this.$message.success("处理完成!");
              this.isShowAddSprcModal = false;
              if ((this.form.productCode = this.selectionRows[0].mcode)) {
                this.$parent.updateBottom();
              }
            } else {
              this.$message.warning("处理失败!");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    addSprcUpdataForm(form) {
      console.log(form);
    },

    showAddSprcModal(record) {
      this.form = this.$options.data().form;
      this.isShowAddSprcModal = true;
      this.form.productCode = record.mcode;
      this.form.productName = record.mname;
      this.form.productSeries = record.mtypename;
      this.form.productSpec = record.mexplain;
      this.form.productStandard = record.sprc;
    },

    resetForm() {
      this.$refs.ruleForm.resetFields();
    },

    limitNumber(value) {
      if (typeof value === "string") {
        return !isNaN(Number(value)) ? value.replace(/\./g, "") : 0;
      } else if (typeof value === "number") {
        return !isNaN(value) ? String(value).replace(/\./g, "") : 0;
      } else {
        return 0;
      }
    },
  },

  components: {},
};
</script>

<style lang="less" scoped>
</style>
